package com.bxl.chooser.factory;

import com.bxl.chooser.bean.AppInfo;
import com.bxl.chooser.bean.ChooseMsg;
import com.bxl.chooser.bean.ChooseResponseMsg;
import com.bxl.chooser.bean.MessageBean;
import com.bxl.chooser.oper.OperDefine;
import com.bxl.netty.rpc.common.message.PackageMsg;
import com.bxl.netty.rpc.common.message.RegisterContent;
import com.bxl.netty.rpc.common.message.RpcHeader;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

/**
 * 为了简单，消息共用了，不该消息共用的
 *
 * @author baixl
 * @date 2021/3/25
 */
public class ChooseMsgFactory {
    public static ChooseMsg createChooseMsg(AppInfo appInfo, String messageId, String content, int commitIndex) {
        return new ChooseMsg(appInfo.getTerm(), appInfo.getVotedFor(), appInfo.getLogIndex(), content, messageId, appInfo.getAppId(), commitIndex);
    }

    public static ChooseMsg createChooseMsgForChoose(AppInfo appInfo, String messageId, String content, int commitIndex) {
        return new ChooseMsg(appInfo.getTerm() + 1, appInfo.getVotedFor(), appInfo.getLogIndex(), content, messageId, appInfo.getAppId(), commitIndex);
    }

    public static ChooseMsg createChooseMsg(AppInfo appInfo, MessageBean messageBean, int commitIndex) {
        return new ChooseMsg(appInfo.getTerm(), appInfo.getVotedFor(), messageBean.getIndex(), messageBean.getContent(), messageBean.getMessageId(), appInfo.getAppId(), commitIndex);
    }

    public static ChooseResponseMsg createChooseResponseMsg(int term, boolean isVote) {
        return new ChooseResponseMsg(term, isVote);
    }


    public static ChooseResponseMsg createChooseResponseMsg(String code, String desc, String messageId, String msgContent) {
        return new ChooseResponseMsg(code, desc, messageId, msgContent);
    }

    public static ChooseResponseMsg createChooseResponseMsg(int term, boolean isVote, int index) {
        return new ChooseResponseMsg(term, isVote, index);
    }

    public static PackageMsg<ChooseMsg> createMsg(int flag, AppInfo appInfo, MessageBean messageBean, int commitIndex) {
        long messageID = Math.abs(UUID.randomUUID().getLeastSignificantBits());
        RpcHeader rpcHeader = new RpcHeader(flag, messageID);
        ChooseMsg rpcContent = createChooseMsg(appInfo, messageBean, commitIndex);
        PackageMsg<ChooseMsg> msg = new PackageMsg<ChooseMsg>();
        msg.setHeader(rpcHeader);
        msg.setContent(rpcContent);
        return msg;
    }

    public static PackageMsg<ChooseMsg> createMsg(int flag, AppInfo appInfo, String messageId, String content, int commitIndex) {
        long messageID = Math.abs(UUID.randomUUID().getLeastSignificantBits());
        RpcHeader rpcHeader = new RpcHeader(flag, messageID);
        ChooseMsg rpcContent = null;
        if (flag == OperDefine.CHOOSER.getOper()) {
            rpcContent = createChooseMsgForChoose(appInfo, messageId, content, commitIndex);
        } else {
            rpcContent = createChooseMsg(appInfo, messageId, content, commitIndex);
        }
        PackageMsg<ChooseMsg> msg = new PackageMsg<ChooseMsg>();
        msg.setHeader(rpcHeader);
        msg.setContent(rpcContent);
        return msg;
    }

    public static PackageMsg<ChooseMsg> createClientMsg(int flag, String content, String messageId) {
        long messageID = Math.abs(UUID.randomUUID().getLeastSignificantBits());
        RpcHeader rpcHeader = new RpcHeader(flag, messageID);
        if (StringUtils.isEmpty(messageId)) {
            messageId = String.valueOf(messageID);
        }
        ChooseMsg rpcContent = new ChooseMsg(0, "", 0, content, messageId, "", 0);
        PackageMsg<ChooseMsg> msg = new PackageMsg<ChooseMsg>();
        msg.setHeader(rpcHeader);
        msg.setContent(rpcContent);
        return msg;
    }
}
